<?php
namespace Admin\Controller;

class AdController extends AdminController
{
    public function index()
    {
        Cookie('__forward__', $_SERVER['REQUEST_URI']);
        session('expire_ad_notice', true);
        $list = $this->lists('ad', null, 'end_time DESC, id DESC');
        foreach ($list as &$item) {
            $time = $item['end_time'];
            if ($time - NOW_TIME > (60*60*24*3)) {
                $item['end_time_text'] = time_format($time);
            } elseif ($time - NOW_TIME > (60*60*24*2)) {
                $item['end_time_text'] = '还剩3天到期'; 
            } elseif ($time - NOW_TIME > (60*60*24*1)) {
                $item['end_time_text'] = '还剩2天到期';
            } elseif (($time - NOW_TIME <= (60*60*24*1)) && ($time - NOW_TIME > 0)) {
                $item['end_time_text'] = '还剩1天到期';
            } else {
                $item['end_time_text'] = '已过期';
            }
        }
        $this->assign('_list', $list);
        $this->meta_title = "广告列表";
        $this->display();
    }
    public function scorelist()
    {
        Cookie('__forward__', $_SERVER['REQUEST_URI']);
        session('expire_ad_notice', true);
        $map['position']=array('eq','score_show_img');
        $list = $this->lists('ad', $map, 'end_time DESC, id DESC');
        foreach ($list as &$item) {
            $time = $item['end_time'];
            if ($time - NOW_TIME > (60*60*24*3)) {
                $item['end_time_text'] = time_format($time);
            } elseif ($time - NOW_TIME > (60*60*24*2)) {
                $item['end_time_text'] = '还剩3天到期';
            } elseif ($time - NOW_TIME > (60*60*24*1)) {
                $item['end_time_text'] = '还剩2天到期';
            } elseif (($time - NOW_TIME <= (60*60*24*1)) && ($time - NOW_TIME > 0)) {
                $item['end_time_text'] = '还剩1天到期';
            } else {
                $item['end_time_text'] = '已过期';
            }
        }
        $this->assign('_list', $list);
        $this->meta_title = "首页播豆广告列表";
        $this->display();
    }
    public function speciallist()
    {
        Cookie('__forward__', $_SERVER['REQUEST_URI']);
        session('expire_ad_notice', true);
//        $map['type']=array('eq','special');
        $map['position']=array('eq','panic_banner');
        $list = $this->lists('ad', $map, 'end_time DESC, id DESC');
//        dump($list);
        foreach ($list as &$item) {
            $time = $item['end_time'];
            if ($time - NOW_TIME > (60*60*24*3)) {
                $item['end_time_text'] = time_format($time);
            } elseif ($time - NOW_TIME > (60*60*24*2)) {
                $item['end_time_text'] = '还剩3天到期';
            } elseif ($time - NOW_TIME > (60*60*24*1)) {
                $item['end_time_text'] = '还剩2天到期';
            } elseif (($time - NOW_TIME <= (60*60*24*1)) && ($time - NOW_TIME > 0)) {
                $item['end_time_text'] = '还剩1天到期';
            } else {
                $item['end_time_text'] = '已过期';
            }
        }
        $this->assign('_list', $list);
        $this->meta_title = "首页抢购会广告列表";
        $this->display();
    }
    public function speciallist_add(){
        $Ad = D('ad');
        if (IS_POST) {
            $data = $Ad->create();
            $data || $this->error($Ad->getError());
            $id = $Ad->add();
            $id > 0 ? $this->success('新增成功', Cookie('__forward__')) : $this->error('新增失败');
        }
        $this->meta_title = "新增广告";
        $this->display('speciallist_add');
    }
    public function speciallist_edit(){
        $Ad = D('ad');
        $id = I('get.id', 0);
        $info = M('ad')->find($id);
        $info || $this->error('参数有误');
//        echo 123;
//        dump($info);exit;
        if (IS_POST) {
            $data = $Ad->create();
            $data || $this->error($Ad->getError());
            $status = $Ad->where(array('id'=>$id))->save();
            $status !== false ? $this->success('修改成功', Cookie('__forward__')) : $this->error('修改失败');
        }

        $this->assign('info', $info);
        $this->meta_title = "编辑广告";
        $this->display('speciallist_add');
    }
    /**
     * APP启动时广告修改
     */
    public function init(){
        $path = './Data/AppInitC';
//        dump($_POST);
//        exit;
        if(IS_POST) {
            $logo = I('post.logo');
            $title = I('post.title');
            $content = I('post.content');
            $status = I('post.status');
            if($logo && $title && $content){
                $config = array(
                    'logo' => $logo,
                    'title' => $title,
                    'content' => $content,
                    'status'=>$status
                );
                file_put_contents($path,serialize($config));
                $this->success('APP启动广告修改成功！');
            }else{
                $this->error('请完成表单！');
            }
        }ELSE {
            if ( !file_exists ( $path ) ) {
                $config = [
                    'logo'    => '',
                    'title'   => '',
                    'content' => '',
                    'status' => ''
                ];
            } else {
                $config = unserialize ( file_get_contents ( $path ) );
            }
//            if($config['logo']){
//                $config
//            }
            //echo $config['logo'];
            //echo $config['logo_c'] = D('pictrue')->Where('id='.$config['logo'])->getField('path');

            $config['logo_c'] = D('picture')->Where('id='.$config['logo'])->getField('path');
            include 'Ueditor/Ueditor.class.php';
            $ue = new \Ueditor();
            $ue->init();
            $content = $ue->editor('content',$config['content']);
            $this->assign('content',$content);
            $this->assign ( 'config', $config );
            $this->display ('Ad/init');
        }
    }

    public function scoreadd(){
        $Ad = D('ad');
        if (IS_POST) {
            $data = $Ad->create();
            $data || $this->error($Ad->getError());
            $id = $Ad->add();
            $id > 0 ? $this->success('新增成功', Cookie('__forward__')) : $this->error('新增失败');
        }
        $this->meta_title = "新增广告";
        $this->display('scoreedit');
    }
    public function scoreedit(){
        $Ad = D('ad');
        $id = I('get.id', 0);
        $info = M('ad')->find($id);
        $info || $this->error('参数有误');
        
        if (IS_POST) {
            $data = $Ad->create();
            $data || $this->error($Ad->getError());
            $status = $Ad->where(array('id'=>$id))->save();
            $status !== false ? $this->success('修改成功', Cookie('__forward__')) : $this->error('修改失败');
        }
        
        $this->assign('info', $info);
        $this->meta_title = "编辑广告";
        $this->display('scoreedit');
    }
    public function add() {
        $Ad = D('ad');
        if (IS_POST) {
            $data = $Ad->create();
            $data || $this->error($Ad->getError());
            $id = $Ad->add();
            $id > 0 ? $this->success('新增成功', Cookie('__forward__')) : $this->error('新增失败');
        }
        $this->meta_title = "新增广告";
        $this->display('edit');
    }

    public function edit() {
        $Ad = D('ad');
        $id = I('get.id', 0);
        $info = M('ad')->find($id);
        $info || $this->error('参数有误');
        
        if (IS_POST) {
            $data = $Ad->create();
            $data || $this->error($Ad->getError());
            $status = $Ad->where(array('id'=>$id))->save();
            $status !== false ? $this->success('修改成功', Cookie('__forward__')) : $this->error('修改失败');
        }
        
        $this->assign('info', $info);
        $this->meta_title = "编辑广告";
        $this->display('edit');
    }

    public function del()
    {
        $id = I('id');
        if (! $id) {
            $this->error('id不存在', Cookie('__forward__'));
        }
        $m_ad = M("ad");
        $result = $m_ad->where(array(
            'id' => $id
        ))->delete();
        $result && $this->success('删除成功', Cookie('__forward__'));
        $this->error('删除失败', Cookie('__forward__'));
    }

    /**
     * 首页轮播
     */
    public function lunbo(){
        $map['position']=array('eq','index_banner');
        $list=M('ad')->where($map)->select();
        $this->assign('_list',$list);
        $this->meta_title = "首页轮播图";
        $this->display();
    }

    public function lunbo_add(){
        $Ad = D('ad');
        if (IS_POST) {
            $lunbo=array('title'=>I('title'),'sort'=>I('sort'),'position'=>I('position'),'cover_id'=>I('cover_id'),'content'=>I('content'),'create_time'=>NOW_TIME,'update_time'=>NOW_TIME,'begin_time'=>strtotime(I('begin_time')),'end_time'=>strtotime(I('end_time')));
            $id = $Ad->add($lunbo);
            $id > 0 ? $this->success('新增成功',U('lunbo')) : $this->error('新增失败');
        }
        $this->meta_title = "新增轮播";
        $this->display();
    }

    public function lunbo_edit(){
        $Ad = D('ad');
        $id = I('get.id', 0);
        $info = M('ad')->find($id);
        $info || $this->error('参数有误');

        if (IS_POST) {
            $lunbo=array('title'=>I('title'),'sort'=>I('sort'),'position'=>I('position'),'cover_id'=>I('cover_id'),'content'=>I('content'),'update_time'=>NOW_TIME,'begin_time'=>strtotime(I('begin_time')),'end_time'=>strtotime(I('end_time')));
            $status = $Ad->where(array('id'=>$id))->save($lunbo);
            $status !== false ? $this->success('修改成功', U('lunbo')) : $this->error('修改失败');
        }

        $this->assign('info', $info);
        $this->meta_title = "编辑轮播图";
        $this->display('lunbo_add',U('lunbo'));
    }

    public function lunbo_del(){
        $id = I('id');
        if (! $id) {
            $this->error('id不存在', Cookie('__forward__'));
        }
        $m_ad = M("ad");
        $result = $m_ad->where(array(
            'id' => $id
        ))->delete();
        $result && $this->success('删除成功');
        $this->error('删除失败');
    }

    /**
     * 分类表头
     */
    public function categorylisthead(){
        $map['position']=array('eq','category_header');
        $list=M('ad')->where($map)->select();
        $this->assign('_list',$list);
        $this->meta_title = "分类表头";
        $this->display();
    }

    public function categorylisthead_add(){
        $Ad = D('ad');
        if (IS_POST) {
            $lunbo=array('title'=>I('title'),'position'=>I('position'),'cover_id'=>I('cover_id'),'content'=>I('content'),'create_time'=>NOW_TIME,'update_time'=>NOW_TIME,'begin_time'=>strtotime(I('begin_time')),'end_time'=>strtotime(I('end_time')));
            $id = $Ad->add($lunbo);
            $id > 0 ? $this->success('新增成功',U('categoryListHead')) : $this->error('新增失败');
        }
        $this->meta_title = "新增轮播";
        $this->display();
    }

    public function categorylisthead_edit(){
        $Ad = D('ad');
        $id = I('get.id', 0);
        $info = M('ad')->find($id);
        $info || $this->error('参数有误');

        if (IS_POST) {
            $lunbo=array('title'=>I('title'),'position'=>I('position'),'cover_id'=>I('cover_id'),'content'=>I('content'),'update_time'=>NOW_TIME,'begin_time'=>strtotime(I('begin_time')),'end_time'=>strtotime(I('end_time')));
            $status = $Ad->where(array('id'=>$id))->save($lunbo);
            $status !== false ? $this->success('修改成功', U('categorylisthead')) : $this->error('修改失败');
        }

        $this->assign('info', $info);
        $this->meta_title = "编辑轮播图";
        $this->display('categorylisthead_add',U('categorylisthead'));
    }

    public function categorylisthead_del(){
        $id = I('id');
        if (! $id) {
            $this->error('id不存在', Cookie('__forward__'));
        }
        $m_ad = M("ad");
        $result = $m_ad->where(array(
            'id' => $id
        ))->delete();
        $result && $this->success('删除成功');
        $this->error('删除失败');
    }
}
?>